// 搜索数据
const searchData = [
    {
        title: "学习范围",
        description: "大数据与人工智能领域的知识体系，包括数学基础、编程工具、机器学习算法等核心内容",
        url: "pages/overview/experience.html",
        category: "导航"
    },
    {
        title: "学习方法",
        description: "数据分析与机器学习入门攻略，AI算法学习路线图，持续学习的方法与工具推荐",
        url: "pages/overview/methods.html",
        category: "导航"
    },
    {
        title: "心得感悟",
        description: "在AI与大数据领域的实践思考，技术选型的经验总结，职业发展的规划与建议",
        url: "pages/overview/thoughts.html",
        category: "导航"
    },
    {
        title: "Python数据分析笔记",
        description: "包含Pandas、NumPy等库的使用技巧和实战案例",
        url: "pages/notes/python-data-analysis.html",
        category: "笔记"
    },
    {
        title: "机器学习算法笔记",
        description: "常用机器学习算法原理与实现详解",
        url: "pages/notes/machine-learning.html",
        category: "笔记"
    },
    {
        title: "深度学习算法笔记",
        description: "CNN、RNN、Transformer等深度学习模型架构与应用",
        url: "pages/notes/deep-learning.html",
        category: "笔记"
    },
    {
        title: "大数据技术笔记",
        description: "Hadoop、Spark等大数据框架的实践经验总结",
        url: "pages/notes/big-data.html",
        category: "笔记"
    },
    {
        title: "Python基础教程",
        description: "黑马程序员Python编程基础入门课程",
        url: "pages/videos/python-basics.html",
        category: "视频"
    },
    {
        title: "Python机器学习入门",
        description: "3天快速入门Python机器学习课程",
        url: "pages/videos/machine-learning-basics.html",
        category: "视频"
    },
    {
        title: "Python数据挖掘入门",
        description: "4天快速入门Python数据挖掘课程",
        url: "pages/videos/data-mining-basics.html",
        category: "视频"
    }
];

// 搜索功能实现
function performSearch(searchTerm) {
    const searchResults = searchData.filter(item => {
        const searchString = `${item.title} ${item.description} ${item.category}`.toLowerCase();
        return searchString.includes(searchTerm.toLowerCase());
    });
    return searchResults;
}

// 创建搜索结果HTML
function escapeHtml(unsafe) {
    return unsafe
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#039;");
}

function createSearchResultHTML(result) {
    return `
        <a href="${escapeHtml(result.url)}" 
           class="block p-4 hover:bg-gray-800 transition-colors">
            <h3 class="font-semibold text-white">${escapeHtml(result.title)}</h3>
            <p class="text-sm text-gray-400 mt-1">${escapeHtml(result.description)}</p>
        </a>
    `;
}

// 初始化搜索功能
document.addEventListener('DOMContentLoaded', () => {
    const searchInput = document.getElementById('search-input');
    const searchResults = document.getElementById('search-results');
    const searchDropdown = document.getElementById('search-dropdown');
    
    // 处理搜索输入
    searchInput.addEventListener('input', (e) => {
        const searchTerm = e.target.value.trim();
        
        if (searchTerm.length > 0) {
            const results = performSearch(searchTerm);
            
            if (results.length > 0) {
                searchResults.innerHTML = results.map(createSearchResultHTML).join('');
                searchDropdown.classList.remove('hidden');
            } else {
                searchResults.innerHTML = `
                    <div class="p-4 text-gray-400 text-center">
                        未找到相关内容
                    </div>
                `;
                searchDropdown.classList.remove('hidden');
            }
        } else {
            searchDropdown.classList.add('hidden');
        }
    });
    
    // 点击外部关闭搜索结果
    document.addEventListener('click', (e) => {
        if (!searchInput.contains(e.target) && !searchDropdown.contains(e.target)) {
            searchDropdown.classList.add('hidden');
        }
    });
}); 